#include <func.h>

#define MAXLINE 1024

int main(int argc, char* argv[])
{
    // 1. 先pipe
    int fields[2];
    if (pipe(fields) == -1) {
        perror("pipe()");
        exit(1);
    }
    // 2. 再fork
    pid_t pid = fork();
    switch (pid) {
    case -1:
        perror("fork()");
        exit(1);
    case 0: // 子进程关闭写端
        close(fields[1]);
        char message[MAXLINE];
        read(fields[0], message, MAXLINE);
        printf("message from parent: %s", message);
        break;
    default: // 父进程关闭读端
        close(fields[0]);
        /* for(;;) ; */
        write(fields[1], "Hello from parent", 18);
        break;
    }
    return 0;
}

